<template>
  <div class='container'>
    <div style="margin-bottom: 12px;">
      <el-button style="margin-right: 10px;" @click="handleCreate" type="success" icon="el-icon-plus">新增</el-button>
      <el-button style="margin-right: 10px;" @click="handleDelete(multipleSelection)" type="danger"
                 icon="el-icon-delete">删除
      </el-button>
    </div>
    <el-table stripe
              :data="tableData" ref="table" v-loading="listLoading" element-loading-text="加载中..." border fit
              highlight-current-row @selection-change="handleSelectionChange"
              style="max-width: 100%"
    >
      <el-table-column align="center" type="selection" width="55">
      </el-table-column>
      <!--<el-table-column align="center" type="index" label="序号" width="55">-->
      <!--</el-table-column>-->
      <el-table-column align="center" label="渠道名称">
        <template slot-scope="scope">
          <span>{{formatDictId(scope.row.dictId)}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="原创板块渲染模板（前串）">
        <template slot-scope="scope">
          <span>{{scope.row.originalPlateTemplatePrefix}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="原创板块渲染模板（后串）">
        <template slot-scope="scope">
          <span>{{scope.row.originalPlateTemplateSuffix}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="行业板块渲染模板（前串）">
        <template slot-scope="scope">
          <span>{{scope.row.industryPlateTemplatePrefix}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="行业板块渲染模板（后串）">
        <template slot-scope="scope">
          <span>{{scope.row.industryPlateTemplateSuffix}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="概念板块渲染模板（前串）">
        <template slot-scope="scope">
          <span>{{scope.row.conceptPlateTemplatePrefix}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="概念板块渲染模板（后串）">
        <template slot-scope="scope">
          <span>{{scope.row.conceptPlateTemplateSuffix}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="股票渲染模板（前串）">
        <template slot-scope="scope">
          <span>{{scope.row.stockTemplatePrefix}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="股票渲染模板（后串）">
        <template slot-scope="scope">
          <span>{{scope.row.stockTemplateSuffix}}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center" label="编辑" width="135" class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              style="width: auto" class="button-icon"
              icon="el-icon-edit" type="primary" size="mini" @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              style="width: auto" class="button-icon"
              icon="el-icon-delete" type="danger" size="mini" @click="handleDelete([scope.row.id])"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="listQuery.pageNum"
        :page-sizes="[10,20,30, 50]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="200px"
        style='margin: 24px;'
      >
        <el-form-item label="渠道名称" prop="dictId">
          <el-select v-model="temp.dictId" type="text" placeholder="请输入" style="width: 250px">
            <el-option v-for="item in dataDict.tag_compiler" :key="item.dictKey" :value="item.dictKey"
                       :label="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="原创板块渲染模板（前串）" prop="originalPlateTemplatePrefix">
          <el-input v-model="temp.originalPlateTemplatePrefix" type="text" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="原创板块渲染模板（后串）" prop="originalPlateTemplateSuffix">
          <el-input v-model="temp.originalPlateTemplateSuffix" type="text" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="行业板块渲染模板（前串）" prop="industryPlateTemplatePrefix">
          <el-input v-model="temp.industryPlateTemplatePrefix" type="text" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="行业板块渲染模板（后串）" prop="industryPlateTemplateSuffix">
          <el-input v-model="temp.industryPlateTemplateSuffix" type="text" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="概念板块渲染模板（前串）" prop="conceptPlateTemplatePrefix">
          <el-input v-model="temp.conceptPlateTemplatePrefix" type="text" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="概念板块渲染模板（后串）" prop="conceptPlateTemplateSuffix">
          <el-input v-model="temp.conceptPlateTemplateSuffix" type="text" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="股票渲染模板（前串）" prop="stockTemplatePrefix">
          <el-input v-model="temp.stockTemplatePrefix" type="text" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="股票渲染模板（后串）" prop="stockTemplateSuffix">
          <el-input v-model="temp.stockTemplateSuffix" type="text" placeholder="请输入">
          </el-input>
        </el-form-item>
        <el-form-item label="【提示】">
          <span>股票和板块代码模板请用</span><span size="mini" class="code">%CODE%</span><span>股票和板块名称模板请用</span><span
          size="mini" class="code">%NAME%</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{$t('table.cancel')}}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
        <el-button v-else type="primary" @click="updateData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    getTypeDict,
    tagCompilerList,
    tagCompilerAdd,
    tagCompilerUpdate,
    tagCompilerDelete
  } from '../../api/systemManagement'

  export default {
    name: 'List',
    created() {
      this.getDict()
      this.getList()
    },
    methods: {
      getDict() {
        return getTypeDict({ type: 'tag_compiler' }).then(response => {
          this.dataDict.tag_compiler = response.Data
        })
      },
      formatDictId(dictId) {
        for (let i = 0; i < this.dataDict.tag_compiler.length; i++) {
          if (this.dataDict.tag_compiler[i].dictKey === dictId) {
            return this.dataDict.tag_compiler[i].dictValue
          }
        }
        return dictId + '(字典已删除)'
      },
      handleSelectionChange(val) {
        this.multipleSelection = val.map(e => e.id)
      },
      handleDelete(e) {
        if (e.length === 0) return
        this.$confirm('确认删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          tagCompilerDelete({ batchIds: e }).then(() => {
            this.getList()
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      getList() {
        this.listLoading = true
        const params = {
          pageNum: this.listQuery.pageNum,
          pageSize: this.listQuery.pageSize
        }
        tagCompilerList(params).then(response => {
          this.tableData = response.Data
          this.total = response.PageInfo.Total * 1
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val
        this.getList()
      },
      searchWord(data) {
        this.showButton = true
        this.listQuery.parentId = data.id
        this.tableName = data.name
        this.getList()
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            tagCompilerAdd(this.temp).then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      handleUpdate(row) {
        this.temp = Object.assign({}, row) // copy obj
        this.temp.timestamp = new Date(this.temp.timestamp)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            tagCompilerUpdate(this.temp).then(() => {
              this.getList()
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '更新成功',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      resetTemp() {
        this.temp = {
          id: '',
          dictId: '',
          originalPlateTemplatePrefix: '',
          originalPlateTemplateSuffix: '',
          industryPlateTemplatePrefix: '',
          industryPlateTemplateSuffix: '',
          conceptPlateTemplatePrefix: '',
          conceptPlateTemplateSuffix: '',
          stockTemplatePrefix: '',
          stockTemplateSuffix: ''
        }
      }
    },
    data() {
      return {
        total: 0,
        listLoading: false,
        showButton: false,
        tableName: '标签润色管理',
        multipleSelection: [],
        listQuery: {
          pageNum: 1,
          pageSize: 20
        },
        textMap: {
          update: '编辑',
          create: '新增'
        },
        dialogStatus: '',
        dialogFormVisible: false,
        tableData: [],
        temp: {
          id: '',
          dictId: '',
          originalPlateTemplatePrefix: '',
          originalPlateTemplateSuffix: '',
          industryPlateTemplatePrefix: '',
          industryPlateTemplateSuffix: '',
          conceptPlateTemplatePrefix: '',
          conceptPlateTemplateSuffix: '',
          stockTemplatePrefix: '',
          stockTemplateSuffix: ''
        },
        rules: {
          dictId: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }],
          originalPlateTemplatePrefix: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }],
          originalPlateTemplateSuffix: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }],
          industryPlateTemplatePrefix: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }],
          industryPlateTemplateSuffix: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }],
          conceptPlateTemplatePrefix: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }],
          conceptPlateTemplateSuffix: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }],
          stockTemplatePrefix: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }],
          stockTemplateSuffix: [{ type: 'string', required: true, message: '请输入...', trigger: 'change' }]
        },
        dataDict: {
          tag_compiler: []
        }
      }
    }
  }
</script>

<style>
  .el-dialog {
    width: 900px;
  }

  .code {
    background-color: #162532;
    padding: 0 5px;
    margin: 0 5px;
    color: #fff;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
  }
</style>
